#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/**
 * Definition for singly-linked list.
 */
struct ListNode {
	int val;
	struct ListNode* next;
};
typedef struct ListNode listNode, *listNode_P;

int main()
{
	return 0;
}

struct ListNode* mergeKLists(struct ListNode** lists, int listsSize)
{
	/* printf("listsSize:\t%d\n", listsSize); */
	if (!listsSize) return NULL;
	if (listsSize == 1) return *lists;

	listNode_P p = 0, res;

	while (true) {
		int idx = 0, min = INT_MAX;
		for (int i = 0; i < listsSize; i++)
			if (!lists[i]) continue;
			else if (lists[i]->val < min) {
				idx = i;
				min = lists[i]->val;
			}
		min = INT_MAX;

		listNode_P remove = lists[idx];
		if (!remove) break;
		/* printf("%d\n", remove->val); */
		lists[idx] = lists[idx]->next;

		if (!p) {
			p = remove;
			res = p;
		} else {
			p->next = remove;
			p = p->next;
		}
	}

	return res;
}
